<?php

namespace App\Http\Controllers\Admin;

use App\Models\Project_admin_node;
use App\Models\Project_admin_role;
use App\Models\Project_admin_role_node;
use App\Models\Project_admin_user;
use App\Models\Project_admin_user_role;
use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class DistriController extends Controller
{

    /**
     * 分配角色
     * @param $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function loadRole($id)
   {
      $role = project_admin_role::all(); //获取所有的角色信息

       $userRole = project_admin_user_role::getRole($id);  //用户角色可能为空
        $uid = $id;
       return view('admin/role/index',compact('uid','role','userRole'));
   }

    /**
     * 分配角色: 动作
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function saveRole(Request $request)
    {

       $true = project_admin_user_role::distRole($request); //给用户分配角色
        if ($true) {
            return redirect(route('admin.welcome'))->with('success', '分配角色成功');

        } else {
            return redirect(route('admin.welcome'))->with('error', '分配角色失败');

        }
    }

    /**
     * 分配节点
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function loadNode(Request $request)
    {
        $rId = $request ->id ; //获取当前角色的id
        $node = project_admin_node::all();  //获取所有的节点

        $rNid =project_admin_role_node::rGetNid($rId); //获取当前选中角色对应的节点id

        return view('admin.node.index',compact('rId','node','rNid'));

    }

    /**
     * 节点保存:动作
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function saveNode(Request $request)
    {
       $true = project_admin_role_node::saveRidNid($request);

       if($true){
          return  redirect(route('admin.role'))->with('success','分配节点成功');
       } else {
           return  redirect(route('admin.role'))->with('success','分配节点失败');
       }
    }

}
